package Frame;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.filechooser.FileSystemView;

import Function.FCMapLibrary;

public class FCNewMap extends JDialog {
	private JFileChooser filec = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory()) {
		protected javax.swing.JDialog createDialog(java.awt.Component parent) throws java.awt.HeadlessException {
			JDialog jd = super.createDialog(parent);
			jd.setSize(500, 400);
			jd.setTitle("选择文件");
			jd.setResizable(false);
			jd.setLocationRelativeTo(null);
			return jd;
		};
	};

	public FCNewMap(FCMapLibrary mapl, MapEditJpanel jpa, JTextField mapname, JLabel mapsize, JTextArea mapinfo) {
		setTitle("新建地图");
		setModal(true);
		setBounds(0, 0, 215, 460);
		setLayout(null);
		setLocationRelativeTo(null);
		setResizable(false);

		JLabel qw = new JLabel("地图名称", JLabel.CENTER);
		qw.setBounds(10, 10, 190, 20);
		this.add(qw);
		JTextField as = new JTextField();
		as.setBounds(10, 35, 190, 20);
		this.add(as);

		JLabel a = new JLabel("地图尺寸", JLabel.CENTER);
		a.setBounds(10, 65, 190, 20);
		this.add(a);
		JLabel b = new JLabel("W:");
		b.setBounds(10, 90, 20, 20);
		this.add(b);
		JTextField c = new JTextField();
		c.setBounds(30, 90, 70, 20);
		this.add(c);
		JLabel d = new JLabel("H:");
		d.setBounds(110, 90, 20, 20);
		this.add(d);
		JTextField e = new JTextField();
		e.setBounds(130, 90, 70, 20);
		this.add(e);

		JLabel we = new JLabel("描述", JLabel.CENTER);
		we.setBounds(10, 120, 190, 20);
		this.add(we);
		JTextArea sd = new JTextArea();
		sd.setBounds(10, 145, 190, 190);
		sd.setText("null");
		this.add(sd);

		JLabel f = new JLabel("大背景材质", JLabel.CENTER);
		f.setBounds(10, 345, 190, 20);
		this.add(f);
		JTextField ff = new JTextField();
		ff.setBounds(10, 370, 120, 20);
		ff.setDisabledTextColor(Color.black);
		ff.setEditable(false);
		this.add(ff);
		JButton fff = new JButton("选择");
		fff.setBounds(130, 370, 70, 20);
		this.add(fff);
		fff.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (filec.showOpenDialog(null) == filec.APPROVE_OPTION)
					ff.setText(filec.getSelectedFile().getPath().replaceAll("\\\\", "/"));
			}
		});

		JButton zx = new JButton("确认");
		zx.setBounds(10, 400, 190, 20);
		this.add(zx);

		zx.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent ee) {
				if (as.getText() != null && !as.getText().equals("")) {
					if (c.getText() != null && !c.getText().equals("") && e.getText() != null
							&& !e.getText().equals("")) {
						if (ff.getText() != null && !ff.getText().equals("")) {
							if (!mapl.isMapRepeat(as.getText())) {
								if (jpa.isSave()) {
									mapl.createMap(as.getText(), Integer.valueOf(e.getText()),
											Integer.valueOf(c.getText()), sd.getText(), ff.getText());
									new FCTip("新建地图", "新建成功!");
									jpa.setNewMap(mapl.getMapOfName(as.getText()));
									jpa.setSave();
									mapname.setText(as.getText());
									mapsize.setText(c.getText() + "*" + e.getText());
									mapinfo.setText(sd.getText());
									dispose();
								} else
									new FCTip("新建地图", "当前地图没有保存!");
							} else
								new FCTip("新建地图", "地图名称重复!");
						} else
							new FCTip("新建地图", "未选择大背景材质!");
					} else
						new FCTip("新建地图", "请输入地图尺寸!");
				} else
					new FCTip("新建地图", "名称不得为空!");
			}
		});

		setVisible(true);
	}
}
